package validate

/**
 *  author : Tuo
 *  e-mail : tuojie@ijovo.com
 *  time   : 2021/12/10 11:42 上午
 *  desc   : 验证器
 *  version: 1.0
 */

import (
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type validator interface {
	validate() error
}

// Interceptor 参数拦截器
var Interceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
	if r, ok := req.(validator); ok {
		if err := r.validate(); err != nil {
			return nil, status.Error(codes.InvalidArgument, err.Error())
		}
	}
	return handler(ctx, req)
}
